const knex = require("../../models/knex");
const EduType = require("../../models/type");
const ModelMd = require("../../models/md");
const ModelComment = require("../../models/comment");
const logger = require("../../logger");

const service = {
  // 增加一个分类
  async addType(body) {
    if ((body.title ?? "") === "") {
      throw Error("分类标题为空");
    }
    if ((body.slug ?? "") === "") {
      throw Error("分类Slug为空");
    }
    const data = {
      title: body.title || "",
      description: body.description || "",
      slug: body.slug || "",
      createTime: new Date(),
      updateTime: new Date(),
    };
    return await EduType.insert(data);
  },
  async modType(body) {
    const data = {
      ...body,
      id: void "11111",
    };
    return await EduType.update(body.id, data);
  },
  async delType(body) {
    const mds = await ModelMd.me().where({tid: body.id}).count({ count: "id" })
    console.log(mds);
    if (mds.length > 0 && mds[0].count > 0) {
        throw Error("该分类下还有文章，无法删除")
    }
    return EduType.delete(body.id);
  },
  async getTypes(body) {
    logger.info("body ====> " + JSON.stringify(body));
    const select = body?.select ?? "";
    if (select) {
      const _select = select.split(",");
      return await EduType.listColumn(_select);
    } else {
      return await EduType.list();
    }
  },

  async addMd(body, user) {
    if ((body.slug ?? "") === "") {
      throw Error("文章Slug为空");
    }
    if ((body.title ?? "") === "") {
      throw Error("文章标题为空");
    }
    if ((body.md ?? "") === "") {
      throw Error("文章内容为空");
    }
    if ((body.tid ?? "") === "") {
      throw Error("文章分类为空");
    }
    const data = {
      slug: body?.slug || "",
      title: body?.title || "",
      md: body?.md || "",
      uid: user.id,
      tid: body?.tid || "",
      tags: body?.tags || "",
      views: body?.views || "0",
      allowComment: body?.allowComment || "0",
      password: body?.password || "",
      weight: +body?.weight ?? 0,
      createTime: new Date(),
      updateTime: new Date(),
    };
    return await ModelMd.insert(data);
  },
  async modMd(body) {
    const data = {
      ...body,
      id: void "heyunmu",
    };
    return await ModelMd.update(body.id, data);
  },
  async delMd(body) {
    await ModelComment.me().where("aid", "=", body.id).del();
    return await ModelMd.delete(body.id);
  },
  async getMd(body) {
    return ModelMd.getById(body.id);
  },
  async getMds(body) {
    return await ModelMd.page(
      body?.current ?? 1,
      body?.size ?? 10,
      [
        "id",
        "slug",
        "title",
        "uid",
        "tid",
        "tags",
        "views",
        "allowComment",
        "password",
        "weight",
      ],
      (table) => knex(table).whereILike("title", `%${body?.title ?? ""}%`)
    );
  },

  async addComment(body) {
    const data = {
      email: body?.email || "",
      name: body?.name || "",
      url: body?.url || "",
      text: body?.text || "",
      pid: body?.pid || "",
      aid: body?.aid || "",
      isAuthor: body?.isAuthor || "",
      createTime: new Date(),
      updateTime: new Date(),
    };
    return await ModelComment.insert(data);
  },
  async modComment(body) {
    const data = {
      ...body,
      id: void "heyunmu",
    };
    return await ModelComment.update(body.id, data);
  },
  async delComment(body) {
    return ModelComment.delete(body.id);
  },
  async getComments(body) {
    return await ModelComment.page(
      body?.current ?? 1,
      body?.size ?? 10,
      ["id", "email", "name", "url", "text", "pid", "aid", "isAuthor"],
      (table) => knex(table).whereILike("text", `%${body?.text ?? ""}%`)
    );
  },
};

module.exports = service;
